var postsData = require('../../../data/posts-data.js')
var app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var globalData = app.globalData;
    var postId = options.id; //获取post.js跳转详情页传递过来的id
    this.setData({
      currentPostId: postId
    });
    // console.log(postId);
    var postData = postsData.postList[postId];
    // console.log(postData);
    this.setData({
      postData
    }); //放入到数据绑定变量中

    // var postCollected = {
    //   1: "true",
    //   2: "flase",
    //   3: "true",
    //   ...
    // }



    //从缓存中读取是否收藏
    var postsCollected = wx.getStorageSync('posts_Collected');
    if (postsCollected) {
      var postCollected = postsCollected[postId];
      if (postCollected) {
        this.setData({
          collected: postCollected
        });
      }
    } else {
      var postsCollected = {}
      postsCollected[postId] = false;
      wx.setStorageSync('posts_Collected', postsCollected);
    }
    // wx.setStorage({
    //   key: 'yinlei',
    //   data: 'hhaa',
    // })

    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId=== postId){
      this.setData({
        isPlayingMusic: true
      })
    }

    //监听总的音乐开关
    var that = this;
    wx.onBackgroundAudioPlay(function(){
      that.setData({
        isPlayingMusic: true
      });
      app.globalData.g_isPlayingMusic =true;
      app.globalData.g_currentMusicPostId = that.data.currentPostId;
    });
    wx.onBackgroundAudioPause(function(){
      that.setData({
        isPlayingMusic: false
      });
      app.globalData.g_isPlayingMusic =false;
      app.globalData.g_currentMusicPostId = null;

    });
  },


  onCollectionTap: function(event) {
    var postsCollected = wx.getStorageSync('posts_Collected');
    var postCollected = postsCollected[this.data.currentPostId];
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected;

    this.showToast(postsCollected, postCollected);
    // wx.getStorage({
    //   key: 'yinlei',
    //   success: function(res) {
    //     console.log(res);
    //   },
    // })
  },

  showToast: function(postsCollected, postCollected) {
    wx.setStorageSync('posts_Collected', postsCollected);
    this.setData({
      collected: postCollected
    });
    wx.showToast({
      title: postCollected ? "收藏成功！" : "取消收藏",
      duration: 1000,
      icon: 'success',
      success: function() {

      },
      fail: function() {

      },
      complete: function() {

      }
    })
  },

  showModal(postsCollected, postCollected) {
    var that = this;
    wx.showModal({
      title: '收藏',
      content: postsCollected ? '收藏该文章?' : '取消收藏该文章？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: 'red',
      confirmText: '确认',
      confirmColor: '#405f80',
      success: function(res) {
        if (res.confirm) {
          wx.setStorageSync('posts_Collected', postsCollected);
          that.setData({
            collected: postCollected
          });
        }
      }
    });
  },

  onShareTap: function(event) {
    var itemList = [
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博",
    ];
    wx.showActionSheet({
      itemList: itemList,
      itemColor: '#405f80',
      success: function(res) {
        // res.cancel
        // res.tapIndex
        wx.showModal({
          title: '用户 ' + itemList[res.tapIndex],
          content: '分享功能暂未开放！',
        })
      }
    })


    // wx.removeStorage({
    //   key: 'yinlei',
    //   success: function(res) {},
    // })

    //清楚所有缓存
    // wx.clearStorage();
  },

  onMusicTap: function(event) {
    var currentPostId = this.data.currentPostId;
    var isPlayingMusic = this.data.isPlayingMusic;
    var postDdata = postsData.postList[currentPostId].music;
    if(isPlayingMusic){
      wx.pauseBackgroundAudio();
      this.setData({
        isPlayingMusic: false
      });
    }else{
      wx.playBackgroundAudio({
        dataUrl: postDdata.url,
        title: postDdata.title,
        coverImgUrl: postDdata.coverImg
      });
      this.setData({
        isPlayingMusic: true
      });
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})